import {request} from "./api";

export const initMenu = (router,store) => {
  if (store.state.routes.length > 0) {
    return;
  }
    request({
      url: "/rights",
      params: {
        roleId:1,
      }
    }).then(res => {
      if (res) {
        //格式化Router
        let fmtRoutes = formatRoutes(res);
        console.log(fmtRoutes)
        //添加到router
        router.addRoutes(fmtRoutes);
        //将数据存入vuex
        store.commit('initRoutes', fmtRoutes)
      }
    });
}
export const formatRoutes =(router) =>{
    let fmtRoutes=[];
    router.forEach(router=>{
      let {
        path,
        component,
        name,
        iconCls,
        children,
      }=router;
      if (children && children instanceof Array){
        children=formatRoutes(children)
      }
      let fmRouter={
        path:path,
        name:name,
        iconCls:iconCls,
        children:children,
        component(resolve){
          if (component.startsWith("Basic")){
            require(['../views/basic/'+component+'.Vue'],resolve);
          }else if (component.startsWith("Mess")){
            require(['../views/carMessage/'+component+'.Vue'],resolve);
          }else if (component.startsWith("Ord")){
            require(['../views/order/'+component+'.Vue'],resolve);
          }else if (component.startsWith("Sto")){
            require(['../views/stock/'+component+'.Vue'],resolve);
          }else if (component.startsWith("Sys")){
            require(['../views/system/'+component+'.Vue'],resolve);
          }
        }
      }
      fmtRoutes.push(fmRouter)
    })
  return fmtRoutes
}